function selectRadioDuree(){
	var choix = document.getElementById("tous");
	var choix2 = document.getElementById("filtrer");

	if(choix.checked){
		envoiDonneeTotal();
		TB = document.getElementById("Tech");
		Radio = document.getElementById("RadioTech");
		OK = document.getElementById("OkTech");
		document.getElementById("RadioDate").style.display = "none";
		TB.style.display = "none";
		OK.style.display = "none";
		Radio.style.display = "none";
		envoiDonneeTotal();
	}

	else{
		RadioTech = document.getElementById("RadioTech");
		RadioDate = document.getElementById("RadioDate");
		TB = document.getElementById("Tech");
		OK = document.getElementById("OkTech");
		OKD = document.getElementById("OkDate");
		SelectMois = document.getElementById("Mois");
		RadioTech.style.display = "block";
		RadioDate.style.display = "block";
		if(RadioTech.checked){
			TB.style.display = "block";
			OK.style.display = "block";
			SelectMois.style.display = "none";
			OKD.style.display = "none";
		}
		if(RadioDate.checked) {

			TB.style.display = "none";
			OK.style.display = "none";
			SelectMois.style.display = "block";
			OKD.style.display = "block";
		}
	}
}

function afficheGraphParTech(){
	var choix = document.getElementById("Tech").value;
	data= {"Tech": choix};
	$.ajax({url:"retourAjax/dataAppelsTemps.php",
		type: "POST",
		data: data
	}).done(function(data){
		data=JSON.parse(data);
		vretour=[];
		test=[]
		for(i=0; i<data.length;i++){
			vretour.push([data[i].date]);
			test.push([data]);
		}

		drawchart(groupeAppel(vretour),dureeAppel(test));

	});


}

function afficheGraphParDate(){
	var choix = document.getElementById("Mois").value;
	alert(choix);
	data= {"Mois": choix};
	$.ajax({url:"retourAjax/dataAppelsTemps.php",
		type: "POST",
		data: data
	}).done(function(data){
		data=JSON.parse(data);
		vretour=[];
		test=[]
		for(i=1; i<data.length;i++){
			vretour.push([data[i].date]);
	
			test.push(data);
		}
		year="2015";
		drawchartMois(groupeAppelMois(vretour,days_in_month(choix,year)),dureeAppelMois(test,days_in_month(choix,year)));

	});


}

function days_in_month(month,year) {
	x = new Date(Date.parse(((month%12)+1).toString() + "/01/" + year)-86400000).getDate();
	vretour=[];
	for(i=0;i<=x;i++){
		if(i<10){
			vretour.push("0"+i+"");
		}
		else{
		vretour.push(""+i+"");
		}
	}
	return vretour;
}

function envoiDonneeTotal(){
	$.ajax({url:"retourAjax/dataAppelsTemps.php"
	}).done(function(data){
		data=JSON.parse(data);
		vretour=[];
		test=[]
		for(i=1; i<data.length;i++){
			vretour.push([data[i].date]);
			test.push([data]);
		}

		drawchart(groupeAppel(vretour),dureeAppel(test));

	});
}

function drawchartMois(data,duree, nbjour){
	$('#TempsNbAppels').highcharts({
		
		xAxis: {

			categories: [nbjour]
		},

		tooltip: {
			pointFormat: '{series.name}: <b>{point.y}</b><br/>',
			valueSuffix: ' ',
			shared: true
		},

		series: [{
			name: 'Nb Appels',
			data: 
				data

		}, {
			name: 'Durée ( min )',
			data: duree
		}]

	});
}

function drawchart(data,duree){
	$('#TempsNbAppels').highcharts({

		xAxis: {
			categories: ['Jan', 'Fev', 'Mar', 'Avr', 'Mai', 'Jui', 'Jui', 'Aou', 'Sep', 'Oct', 'Nov', 'Dec']
		},

		tooltip: {
			pointFormat: '{series.name}: <b>{point.y}</b><br/>',
			valueSuffix: ' ',
			shared: true
		},

		series: [{
			name: 'Nb Appels',
			data: 
				data

		}, {
			name: 'Durée ( min )',
			data: duree
		}]

	});
	
}


function groupeAppel(data){
	month=["01","02","03","04","05","06","07","08","09","10","11","12"]
	listeG=[]
	for(x=0; x<month.length;x++){
		listeMois=[]
		
		for(i=1; i<data.length;i++){
			var date = data[i].toString();
			date = date.split("-");
			if(date[1] == month[x]){
				listeMois.push(date[1])
				

			}
		}
		listeG.push(listeMois.length);
	}
	
	return listeG;
}

function groupeAppelMois(data,jours){
	listeG=[]

	for(x=0; x<jours.length;x++){
		listeJours=[]

		for(i=0; i<data.length;i++){
			var date = data[i].toString();
			date = date.split("-");
			
			date2 = date[2].split(" ");
			
			if(date2[0] == jours[x]){
				
				listeJours.push(date2[0])


			}
			
		}
		listeG.push(listeJours.length);
	}
	return listeG;
}

function dureeAppelMois(data, jours){

	listeG=[]
	
	for(x=0; x<jours.length;x++){
		duree=[];
		var totalduree = 0;
		for(i=0; i<data.length;i++){
			
			var date = data[0][i].date.toString();
			date = date.split("-");
			date2 = date[2].split(" ");
			if(date2[0] == jours[x]){
				var duree = parseInt(data[0][i].duree);
				totalduree = totalduree + duree;
			}
		}
		listeG.push(totalduree);
	}
	return listeG;
}

function dureeAppel(data){
	mois=['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']

	listeG=[]
	for(x=0; x<mois.length;x++){
		duree=[];
		var totalduree = 0;
		for(i=0; i<data.length;i++){
	
			var date = data[0][0][i].date.toString();
			
			date = date.split("-");
			if(date[1] == mois[x]){
				var duree = parseInt(data[0][0][i].duree);
				totalduree = totalduree + duree;
			}


		}

		listeG.push(totalduree);
	}
	
	return listeG;
}

